package com.zjj.edu.web.controller.system;

import com.zjj.edu.common.annotation.Log;
import com.zjj.edu.common.constant.UserConstants;
import com.zjj.edu.common.core.controller.BaseController;
import com.zjj.edu.common.core.domain.AjaxResult;
import com.zjj.edu.common.core.page.TableDataInfo;
import com.zjj.edu.common.core.text.Convert;
import com.zjj.edu.common.enums.BusinessType;
import com.zjj.edu.common.utils.poi.ExcelUtil;
import com.zjj.edu.framework.util.ShiroUtils;
import com.zjj.edu.framework.util.UserPermissionUtil;
import com.zjj.edu.system.domain.SysClassType;
import com.zjj.edu.system.domain.SysUser;
import com.zjj.edu.system.service.ISysClassTypeService;
import com.zjj.edu.system.service.ISysTenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级类型Controller
 *
 * @author zjj
 * @date 2020-06-02
 */
@Controller
@RequestMapping("/system/classType")
public class SysClassTypeController extends BaseController
{
    private String prefix = "system/classType";

    @Autowired
    private ISysClassTypeService sysClassTypeService;

    @Autowired
    private ISysTenantService tenantService;

    @RequiresPermissions("system:classType:view")
    @GetMapping()
    public String classType(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/classType";
    }

    /**
     * 查询班级类型列表
     */
    @RequiresPermissions("system:classType:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysClassType sysClassType)
    {
        startPage();
        sysClassType.setTenantId(ShiroUtils.getTenantId());
        List<SysClassType> list = sysClassTypeService.selectSysClassTypeList(sysClassType);
        return getDataTable(list);
    }

    /**
     * 导出班级类型列表
     */
    @RequiresPermissions("system:classType:export")
    @Log(title = "班级类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysClassType sysClassType)
    {
        sysClassType.setTenantId(ShiroUtils.getTenantId());
        List<SysClassType> list = sysClassTypeService.selectSysClassTypeList(sysClassType);
        ExcelUtil<SysClassType> util = new ExcelUtil<SysClassType>(SysClassType.class);
        return util.exportExcel(list, "classType");
    }

    /**
     * 新增班级类型
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/add";
    }

    /**
     * 新增保存班级类型
     */
    @RequiresPermissions("system:classType:add")
    @Log(title = "班级类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysClassType sysClassType)
    {
        sysClassType.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysClassTypeService.checkNameUnique(sysClassType))) {
            return error("新增班级类型'" + sysClassType.getName() + "'失败，名称已存在");
        }
        sysClassType.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysClassTypeService.insertSysClassType(sysClassType));
    }

    /**
     * 校验名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkTenantNameUnique(SysClassType sysClassType) {
        sysClassType.setTenantId(ShiroUtils.getTenantId());
        return sysClassTypeService.checkNameUnique(sysClassType);
    }

    /**
     * 修改班级类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("sysClassType", sysClassTypeService.selectSysClassTypeById(id));
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/edit";
    }

    /**
     * 修改保存班级类型
     */
    @RequiresPermissions("system:classType:edit")
    @Log(title = "班级类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysClassType sysClassType)
    {
        sysClassType.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysClassTypeService.checkNameUnique(sysClassType))) {
            return error("修改班级类型'" + sysClassType.getName() + "'失败，名称已存在");
        }
        sysClassType.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysClassTypeService.updateSysClassType(sysClassType));
    }

    /**
     * 删除班级类型
     */
    @RequiresPermissions("system:classType:remove")
    @Log(title = "班级类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SysUser user = ShiroUtils.getSysUser();
        SysClassType classType = new SysClassType();
        classType.setIds(Convert.toStrArray(ids));
        classType.setUpdateBy(user.getUserName());
        if (!UserPermissionUtil.checkUserPermiss()) {
            classType.setTenantId(user.getTenantId());
        }
        return toAjax(sysClassTypeService.deleteSysClassTypeByIds(classType));
    }

    /**
     * 状态修改
     */
    @Log(title = "班级类型管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:classType:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysClassType sysClassType)
    {
        sysClassType.setTenantId(ShiroUtils.getTenantId());
        return toAjax(sysClassTypeService.updateSysClassType(sysClassType));
    }

}
